package com.adams.sys.menu.service.impl;

import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.adams.access.db.bean.Menu;
import com.adams.access.db.bean.Right;
import com.adams.encache.mechanism.CacheUtil;
import com.adams.right.dao.RightDao;
import com.adams.sys.menu.dao.MenuDao;
import com.adams.sys.menu.service.MenuService;
import com.adams.utils.Page;

/**
 * 
 * 
 * File Name : MenuServiceImpl.java
 * 
 * @Description : 菜单服务类,提供菜单的增、删、改和查功能
 * @author 902990封文进
 */
@Service("menuService")
@Transactional
public class MenuServiceImpl implements MenuService
{
	@Autowired
	private MenuDao menuDao;

	@Autowired
	private RightDao rightDao;

	public void setRightDao(RightDao rightDao)
	{
		this.rightDao = rightDao;
	}

	public void setMenuDao(MenuDao menuDao)
	{
		this.menuDao = menuDao;
	}

	/**
	 * 
	 * Description : 添加菜单
	 * 
	 * @param menu
	 * 
	 */
	@Transactional(rollbackFor = java.lang.Exception.class)
	public void createMenu(Menu menu)
	{
		menu.setVersion(1);
		menuDao.save(menu);

		createRight(menu.getMenuCode(), menu.getMenuName());
		CacheUtil.clearCacheByClass(Menu.class);
		Cache cache = CacheUtil.getCache("viewservicecache");
		cache.remove("menus");
	}

	/**
	 * 
	 * Description : 创建权限
	 * 
	 * @param code
	 * 
	 */
	private void createRight(String code, String description)
	{
		Right right = new Right();
		right.setRightCode(code);
		right.setDescription(description);
		rightDao.save(right);

		CacheUtil.clearCacheByClass(Right.class);
	}

	/**
	 * 
	 * Description : 添加菜单
	 * 
	 * @param menuCode
	 * @param menuName
	 * @param url
	 * @param parentCode
	 * 
	 */
	@Transactional(rollbackFor = java.lang.Exception.class)
	public void createMenu(String menuCode, String menuName, String url,
			String parentCode)
	{
		Menu menu = new Menu();
		menu.setMenuCode(menuCode);
		menu.setMenuName(menuName);
		menu.setBusiness(menuCode);
		menu.setUrl(url);
		menu.setVisible(true);
		menu.setIconId(300); //默认图标
		menu.setMenuType(null);
		menu.setExpanded(true);
		
		Integer ordinal = this.queryMaxOrdinal();
		if(ordinal != null){
			//排序值设置为最大值加一
			menu.setOrdinal(ordinal + 1);
		}else{
			menu.setOrdinal(1);
		}

		if (parentCode == null)
		{
			menu.setParentId(Long.valueOf("0"));
		} else
		{
			Menu m = queryByCode(parentCode);
			if (m == null)
			{
				menu.setParentId(Long.valueOf("0"));
			} else
			{
				menu.setParentId(m.getId());
			}
		}
		createMenu(menu);
		CacheUtil.clearCacheByClass(Menu.class);
		Cache cache = CacheUtil.getCache("viewservicecache");
		cache.remove("menus");
	}

	/**
	 * 
	 * Description : 更新菜单
	 * 
	 * @param menu
	 * 
	 */
	@Transactional(rollbackFor = java.lang.Exception.class)
	public void updateMenu(Menu menu)
	{
		menuDao.update(menu);

		updateRight(menu.getMenuCode(), menu.getMenuName());
		CacheUtil.clearCacheByClass(Menu.class);
		Cache cache = CacheUtil.getCache("viewservicecache");
		cache.remove("menus");
	}

	/**
	 * 
	 * Description : 修改权限
	 * 
	 * @param code
	 * 
	 */
	private void updateRight(String code, String description)
	{
		Right right = rightDao.queryByCode(code);
		if (right != null)
		{
			right.setDescription(description);
			rightDao.update(right);
			CacheUtil.clearCacheByClass(Right.class);
		}
	}

	/**
	 * 
	 * Description : 根据菜单码和菜单名更新菜单
	 * 
	 * @param menuCode
	 * @param menuName
	 * 
	 */
	@Transactional(rollbackFor = java.lang.Exception.class)
	public void updateMenu(String menuCode, String menuName)
	{
		Menu menu = queryByCode(menuCode);
		if (menu != null)
		{
			menu.setMenuCode(menuCode);
			menu.setMenuName(menuName);
		}
		updateMenu(menu);
		CacheUtil.clearCacheByClass(Menu.class);
		Cache cache = CacheUtil.getCache("viewservicecache");
		cache.remove("menus");
	}

	/**
	 * 
	 * Description : 删除菜单
	 * 
	 * @param menu
	 * 
	 */
	@Transactional(rollbackFor = java.lang.Exception.class)
	public void deleteMenu(Menu menu)
	{
		menuDao.delete(menu);

		deleteRight(menu.getMenuCode());
		CacheUtil.clearCacheByClass(Menu.class);
		Cache cache = CacheUtil.getCache("viewservicecache");
		cache.remove("menus");
	}

	/**
	 * 
	 * Description : 删除权限
	 * 
	 * @param code
	 * 
	 */
	private void deleteRight(String code)
	{
		Right right = rightDao.queryByCode(code);
		if (right != null)
		{
			rightDao.delete(right);
			CacheUtil.clearCacheByClass(Right.class);
		}
	}

	/**
	 * 
	 * Description : 根据菜单码删除菜单
	 * 
	 * @param menuCode
	 * 
	 */
	@Transactional(rollbackFor = java.lang.Exception.class)
	public void deleteMenu(String menuCode)
	{
		Menu menu = queryByCode(menuCode);
		if (menu != null)
		{
			deleteMenu(menu);
			CacheUtil.clearCacheByClass(Menu.class);
			Cache cache = CacheUtil.getCache("viewservicecache");
			cache.remove("menus");
		}
	}

	/**
	 * 
	 * Description : 根据编号查询菜单
	 * 
	 * @param id
	 * @return Menu
	 * 
	 */
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public Menu queryById(Long id)
	{
		return menuDao.get(id);
	}

	/**
	 * 
	 * Description : 根据菜单码查询菜单
	 * 
	 * @param code
	 * @return Menu
	 * 
	 */
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public Menu queryByCode(String code)
	{
		return menuDao.queryByCode(code);
	}

	/**
	 * 
	 * Description : 根据分页查询所有菜单
	 * 
	 * @param page
	 * @return Page<Menu>
	 * 
	 */
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public Page<Menu> queryAllByPage(Page<Menu> page)
	{
		return menuDao.queryAllByPage(page);
	}

	/**
	 * 
	 * Description : 根据父菜单编号查询子菜单列表
	 * 
	 * @param parentId
	 * @return List<Menu>
	 * 
	 */
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<Menu> querySubMenus(Long parentId)
	{
		// return menuDao.querySubMenus(parentId);
		return processMenu(parentId);
	}

	/**
	 * 
	 * Description : 处理菜单
	 * 
	 * @param parentId
	 * 
	 */
	private List<Menu> processMenu(Long parentId)
	{
		Cache cache = CacheUtil.getCache("viewservicecache");
		Element element;
		if (parentId.longValue() == 0)
		{
			// 先清空缓存
			cache.remove("menus");
			// 从数据库中查询，并保存起来
			Page page = new Page();
			page.setPageSize(Integer.MAX_VALUE);
			List<Menu> list = menuDao.queryAllByPage(page).getResultList();
			element = new Element("menus", list);
			cache.put(element);

		} else
		{
			// 先取缓存
			element = cache.get("menus");
			if (element == null)
			{
				// 从数据库中查询，并保存起来
				Page page = new Page();
				page.setPageSize(Integer.MAX_VALUE);
				List<Menu> list = menuDao.queryAllByPage(page).getResultList();
				element = new Element("menus", list);
				cache.put(element);
			}
		}

		// 过滤菜单
		List<Menu> menus = new ArrayList<Menu>();
		for (Menu menu : (List<Menu>) element.getValue())
		{
			if (menu.getParentId().longValue() == parentId.longValue())
			{
				menus.add(menu);
			}
		}

		return menus;
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<Menu> queryAllSubMenus()
	{
		// TODO Auto-generated method stub
		return menuDao.queryAllSubMenus();
	}

	/**
	 * 
	 * Description : 获取所有菜单
	 * 
	 * @return
	 * 
	 */
	public List<Menu> queryAll()
	{
		return menuDao.queryAll();
	}
	
	/**
	 * Description:获取最大排序值
	 * @return
	 */
	public Integer queryMaxOrdinal(){
		return menuDao.queryMaxOrdinal();
	}

}
